/*
 * Copyright 2018 xwc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.xwc.view;

import com.github.xwc.view.util.MyUtils;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.StackLayout;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.media.image.PixelMap;
import ohos.multimodalinput.event.TouchEvent;

/**
 * Created by xwc on 2018/2/24.
 */
public class Shape extends StackLayout implements Component.DrawTask, ComponentContainer.ArrangeListener, Component.TouchEventListener {
    protected final Paint clipPaint = new Paint();
    protected Paint borderPaint = new Paint();
    protected PixelMap mask;
    protected String borderColor;
    protected int borderWidthPx;
    protected int borderDashGap;
    protected int borderDashWidth;
    private onShapeClickListener onShapeClickListener;
    private ClipHelper clipHelper;

    // 默认背景图
    protected int defaultDrawable;
    // 点击背景图
    protected int pressedDrawable;

    protected int drawable;

    // 默认背景色
    protected String defaultColor;
    // 点击背景色
    protected String pressedColor;

    // 网络下载bitmap
    protected PixelMap mBitmap;

    public Shape(Context context) {
        super(context, null);
    }

    public Shape(Context context,AttrSet attrs) {
        this(context, attrs, null);
    }

    public Shape(Context context,AttrSet attrs, String defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);
    }

    private void init( AttrSet attrs) {

        //边框宽度
        borderWidthPx = AttrUtil.getDimension(attrs, "shape_borderWidth", 0);
        //边框颜色
        borderColor = AttrUtil.getStringValue(attrs, "shape_borderColor", "#FF0000");
        //虚线间隙
        borderDashGap = AttrUtil.getDimension(attrs, "shape_borderDashGap", 0);
        //虚线宽度
        borderDashWidth = AttrUtil.getDimension(attrs, "shape_borderDashWidth", 0);
        //默认背景图
        defaultDrawable = 0;//AttrUtil.getMediaId(attrs, "shape_defaultBgd", 0);

        drawable = defaultDrawable;
        //点击时背景图
        pressedDrawable = 0;
        //点击时背景色
        pressedColor = AttrUtil.getStringValue(attrs, "shape_pressedColor", "#ffffff");
        //默认背景色
        defaultColor = AttrUtil.getStringValue(attrs, "shape_defaultColor", "#426ab3");

        clipPaint.setAntiAlias(true);
        clipPaint.setColor(Color.RED);
        clipPaint.setStyle(Paint.Style.FILLANDSTROKE_STYLE);
        clipPaint.setStrokeWidth(1);

        borderPaint.setAntiAlias(true);
        borderPaint.setStyle(Paint.Style.STROKE_STYLE);

        setClickable(true);
        addDrawTask(this);
        setArrangeListener(this);
        setTouchEventListener(this);
    }


    @Override
    public void onDraw(Component component, Canvas canvas) {
        canvas.clipPath(getClipHelper().mPath, Canvas.ClipOp.INTERSECT);
        dispatchDraw(canvas);
    }

    @Override
    public boolean onArrange(int i, int i1, int i2, int i3) {
        calculateLayout();
        return false;
    }

    private void calculateLayout() {
        if (clipHelper != null) {
            final int height = getHeight();
            final int width = getWidth();
            if (width > 0 && height > 0) {
                if (mask != null && !mask.isReleased()) {
                    mask.release();
                }
                if (clipHelper != null) {
                    clipHelper.setUpClipPath(width, height);
                    mask = clipHelper.createMask(width, height);
                }
            }
        }
    }

    /**
     * 无对应API 暂且和 onDraw 一起执行
     * @param canvas
     */
    protected void dispatchDraw(Canvas canvas) {
    }

    @Override
    public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
        switch (touchEvent.getAction()) {
            case TouchEvent.PRIMARY_POINT_DOWN:
                if (pressedDrawable != 0) {
                    drawable = pressedDrawable;
                    invalidate();
                } else if (pressedColor != null) {

                    // 创建背景元素
                    ShapeElement shapeElement = new ShapeElement();
                    // 设置颜色
                    shapeElement.setRgbColor(MyUtils.getRgbColor(pressedColor));
                    setBackground(shapeElement);
                }
                break;
            case TouchEvent.PRIMARY_POINT_UP:
                touchUp();
                if (onShapeClickListener != null) {
                    onShapeClickListener.onClick(this);
                }
                break;
            case TouchEvent.CANCEL:
                touchUp();
                break;
        }
        return false;
    }

    private void touchUp() {
        if (defaultDrawable != 0) {
            drawable = defaultDrawable;
            invalidate();
        } else if (defaultColor != null) {
            // 创建背景元素
            ShapeElement shapeElement = new ShapeElement();
            // 设置颜色
            shapeElement.setRgbColor(MyUtils.getRgbColor(pressedColor));
            setBackground(shapeElement);
        }
    }

    public void setClipHelper(ClipHelper clipHelper) {
        this.clipHelper = clipHelper;
    }

    public ClipHelper getClipHelper() {
        return clipHelper;
    }

    public PixelMap getBitmap() {
        return mBitmap;
    }

    public void setBitmap(PixelMap mBitmap) {
        this.mBitmap = mBitmap;
    }

    public void reDraw() {
        invalidate();
    }

    public void setOnShapeClickListener(Shape.onShapeClickListener onShapeClickListener) {
        this.onShapeClickListener = onShapeClickListener;
    }

    public interface onShapeClickListener {
        void onClick(Component v);
    }
}
